<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Member as MemberModel;
use app\common\model\Message as MessageModel;
use app\common\model\Topic as TopicModel;
use app\common\model\Friends as FriendsModel;

/**
 * 消息控制器
 * Class Message
 * @package app\home\controller
 */
class Message extends Base
{
    /**
     * [getIndex  验证信息列表]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $frmodel = new FriendsModel();
        $mmodel = new MemberModel();
        $this->getPageAndSize($param);
        $mem = $this->getMember($param['token']);
        $map = [];
        try{
            $map['friend_id'] = $mem['id'];
            $count = $frmodel->getCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $frmodel->getByConditon($map,$this->from, $this->size,'id,memid,friend_id,msg,addtime,states,is_read');
            foreach($lists as $k=>$v){
                $member = $mmodel->findData($v['memid'],'id','img,username');
                $http = substr($member['img'],0,4);
                if($http != 'http'){
                    $lists[$k]['img'] = config('group_domain_name').$member['img'];
                }else{
                    $lists[$k]['img'] = $member['img'];
                }

                $lists[$k]['username'] = $member['username'];
            }

        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getIndex  获取动态列表]
     * @param
     * @return json
     * @author [C。]
     */
    public function getTopic()
    {
        $param = input('param.');
        $model = new TopicModel();
        $mmodel = new MemberModel;
        $mem = $this->getMember($param['token']);
        $this->getPageAndSize($param);
        $map = [];
        $map1 = [];

        try{
            $map2['comment_uid'] = $mem['id'];
            $map2['parid'] = ['neq',0];
            $id_arr = $model->getAllData($map2,'id','id');
            $id_arr = array_column($id_arr,'id','id');
            $ids = join($id_arr,',');

            $map['t.memid'] = $mem['id'];
            $map['t.parid'] = ['neq',0];
            $map['t.comment_uid'] = ['neq',$mem['id']];

            $map1['t.parid'] = ['in',$ids];
            $field = 'am.id memid,am.img,am.username,am.introduction,t.addr,t.content,t.imgs,t.addtime,t.id,am.identity,t.parid,t.info';
            $count = $model->getCounts($map,$map1);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getLists($map,$map1,$this->from, $this->size,$field);
            foreach($lists as $k=>$v){
                $par = $model->findData($v['parid'],'id','id,memid,parid,content,comment_uid');
                $par['content'] = urldecode( $par['content']);

                if($par['parid'] == 0){
                    $mem = $mmodel->findData($par['memid'],'id','username,img');
                    $par['username'] = $mem['username'];
                    $http = substr($mem['img'],0,4);
                    if($http != 'http'){
                        $par['img'] = config('group_domain_name').$mem['img'];
                    }else{
                        $par['img'] = $mem['img'];
                    }
                }else{
                    $mem = $mmodel->findData($par['comment_uid'],'id','username,img');
                    $par['username'] = $mem['username'];
                    $http = substr($mem['img'],0,4);
                    if($http != 'http'){
                        $par['img'] = config('group_domain_name').$mem['img'];
                    }else{
                        $par['img'] = $mem['img'];
                    }

                    $par1 = $model->findData($par['parid'],'id','id,memid,parid,content,comment_uid');
                    $par1['content'] = urldecode( $par1['content']);

                    $mem1 = $mmodel->findData($par1['memid'],'id','username,img');
                    $par1['username'] = $mem1['username'];
                    $http = substr($mem1['img'],0,4);
                    if($http != 'http'){
                        $par1['img'] = config('group_domain_name').$mem1['img'];
                    }else{
                        $par1['img'] = $mem1['img'];
                    }
                    $par['par'] = $par1;
                }
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $lists[$k]['img'] = config('group_domain_name').$v['img'];
                }
                $lists[$k]['content'] = urldecode( $v['content']);
                $lists[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                $lists[$k]['par'] = $par;
            }

        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getIndex  获取私信列表]
     * @param
     * @return json
     * @author [C。]
     */
    public function getPletter()
    {
        $param = input('param.');
        $model = new MessageModel();
        $mem = $this->getMember($param['token']);
        $mmodel = new MemberModel();
        $this->getPageAndSize($param);
        try{
            $count = $model->getCounts($mem['id']);//满足条件的总数
            $allpage = intval(ceil($count[0]['num'] / $this->size));//总页数
            $lists = $model->getLists($mem['id'],$this->from, $this->size);
            $data = array();
            foreach($lists as $k=>$v){
                if($v['fromid'] == $mem['id']){
                    $from = $mmodel->findData($v['toid'],'id','id,img,username');
                    $data[$k]['username'] = $from['username'];

                    $http = substr($from['img'],0,4);
                    if($http != 'http'){
                        $data[$k]['img'] = config('group_domain_name').$from['img'];
                    }else{
                        $data[$k]['img'] = $from['img'];
                    }
                    $data[$k]['is_read'] = 1;
                    $data[$k]['userid'] = $from['id'];
                }
                if($v['toid'] == $mem['id']){
                    $from = $mmodel->findData($v['fromid'],'id','id,img,username');
                    $data[$k]['username'] = $from['username'];

                    $http = substr($from['img'],0,4);
                    if($http != 'http'){
                        $data[$k]['img'] = config('group_domain_name').$from['img'];
                    }else{
                        $data[$k]['img'] = $from['img'];
                    }
                    $data[$k]['userid'] = $from['id'];
                    $data[$k]['is_read'] = $v['is_read'];
                }
                $data[$k]['addtime'] = MessageTime($v['addtime']);
                $data[$k]['content'] = urldecode($v['content']);
                $data[$k]['id'] = $v['id'];
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count[0]['num'],//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $data,  //列表数据
        ]);
    }
    /**
     * [getIndex  发送私信]
     * @param
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $model = new MessageModel();
        $mem = $this->getMember($param['token']);
        try{

            $data = array(
                'fromid'=>$mem['id'],
                'toid'=>$param['toid'],
                'content'=>urlencode($param['content']),
                'addtime'=>time()
            );
            if($mem['id'] > $param['toid']){
                $data['union'] = $param['toid'].','.$mem['id'];
            }else{
                $data['union'] = $mem['id'].','.$param['toid'];
            }
            $id = $model->addData($data);
            $data['id'] = $id;
            $data['content'] = urldecode($data['content']);
            $http = substr($mem['img'],0,4);
            if($http != 'http'){
                $data['img'] = config('group_domain_name').$mem['img'];
            }else{
                $data['img'] = $mem['img'];
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '发送失败'.$e->getMessage());
        }
        return admin_json(1, '发送成功',$data);
    }
    /**
     * [getIndex  根据用户id获取某个人的私信]
     * @param
     * @return json
     * @author [C。]
     */
    public function getLetter()
    {
        $param = input('param.');
        $model = new MessageModel();
        $mmodel = new MemberModel();
        $this->getPageAndSize($param);
        $mem = $this->getMember($param['token']);
        $data = [];
        try{
            $count = $model->getCount($mem['id'],$param['id']);//满足条件的总数
            $allpage = intval(ceil($count[0]['num'] / $this->size));//总页数
            $lists = $model->getlist($mem['id'],$param['id'],$this->from, $this->size);
            foreach($lists as $k=>$v){
                if($v['is_read'] == 0){
                    $model->editData(['is_read'=>1,'id'=>$v['id']]);
                }
                $from = $mmodel->findData($v['fromid'],'id','id,img,username');
                if($v['fromid'] == $mem['id']){
                    $http = substr($from['img'],0,4);
                    if($http != 'http'){
                        $data[$k]['img'] = config('group_domain_name').$from['img'];
                    }else{
                        $data[$k]['img'] = $from['img'];
                    }
                    $data[$k]['username'] = $from['username'];
                    $data[$k]['userid'] = $from['id'];
                }
                if($v['toid'] == $mem['id']){
                    $http = substr($from['img'],0,4);
                    if($http != 'http'){
                        $data[$k]['img'] = config('group_domain_name').$from['img'];
                    }else{
                        $data[$k]['img'] = $from['img'];
                    }
                    $data[$k]['username'] = $from['username'];
                    $data[$k]['userid'] = $from['id'];
                }
                $data[$k]['addtime'] = MessageTime($v['addtime']);
                $data[$k]['content'] = urldecode($v['content']);
                $data[$k]['id'] = $v['id'];
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }

        array_multisort(array_column($data, 'id'),SORT_ASC,$data);
        return admin_json(1, '获取成功',[
            'count' => $count[0]['num'],//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $data,  //列表数据
        ]);
    }
    /**
     * [getDel  删除某条私信]
     * @param
     * @return json
     * @author [C。]
     */
    public function getDel()
    {
        $param = input('param.');
        $model = new MessageModel();
        try{
            $model->deleteFalseData(['id'=>$param['id']],['states'=>0]);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '删除失败'.$e->getMessage());
        }
        return admin_json(1, '删除成功');
    }
    /**
     * [getDel  是否存在未读信息私信]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIsread()
    {
        $param = input('param.');
        $model = new MessageModel();
        $mem = $this->getMember($param['token']);
        $map = [];
        $data = [];
        try{
            $map['toid'] = $mem['id'];
            $map['is_read'] = 0;
            $new_mess = $model->getOne($map);
            if(!empty($new_mess)){
                $data['is_read'] = 1;
            }else{
                $data['is_read'] = 0;
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
}